<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">

<div id="app">

    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="权限名" style="width: 200px;" class="filter-item"
                          v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleAdd()">新建权限</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="list" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="权限名" align="center"></el-table-column>
                <el-table-column prop="keyword" label="权限关键词" align="center"></el-table-column>
                <el-table-column prop="description" label="权限描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row.id)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <div class="add-form">
                <el-dialog title="新建权限" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="permission" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名" prop="name">
                                    <el-input v-model="permission.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限键词" prop="keyword">
                                    <el-input v-model="permission.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="permission.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="addPermission()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <div class="edit-form">
                <el-dialog title="新建权限" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataAddForm" :model="permission" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="权限名" prop="name">
                                    <el-input v-model="permission.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="权限键词" prop="keyword">
                                    <el-input v-model="permission.keyword"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="权限描述">
                                    <el-input v-model="permission.description"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="editPermission()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],
            permission: {},
            dialogFormVisible: false ,
            dialogFormVisible4Edit: false
        },
        mounted() {
            this.findPage();
        },
        methods: {
            findPage() {
                //封装分页参数
                let param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                //发送ajax请求，提交分页相关参数
                axios.post("/permission/findPage.do",param).then((res)=>{
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    this.pagination.total=res.data.total;
                    /*if (this.pagination.total <= 10) {
                        this.handleCurrentChange(1);
                    }*/
                    this.list=res.data.rows;
                })
            },
            handleCurrentChange(currentPage) {
                //currentPage为切换后的页码
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.permission={};//清除表单数据

            },
            handleAdd() {
                this.dialogFormVisible = true;
                //重置数据
                this.resetForm();
            },
            addPermission() {
                axios.post("/permission/add.do",this.permission).then((res)=>{
                    //关闭新增窗口
                    this.dialogFormVisible=false;
                    if (res.data.flag){//执行成功
                        //新增成功后，重新调用分页查询方法，查询出最新的数据
                        this.findPage();
                        //弹出提示信息
                        this.$message({
                            message:res.data.message,
                            type:'success'
                        });
                    }else {//执行失败
                        this.$message.error(res.data.message);
                    }
                })
            },
            editPermission(){
                axios.post("/permission/edit.do",this.permission).then((res)=>{
                    if (res.data.flag){
                        //编辑成功
                        this.$message({
                            type: 'success',
                            message: res.data.message
                        });
                    }else {
                        //编辑失败
                        this.$message.error(res.data.message);
                    }
                }).finally(()=>{
                    //关闭窗口
                    this.dialogFormVisible4Edit=false;
                    //查询
                    this.findPage();
                })
            },
            handleDelete(id){
                this.$confirm("确认删除吗？","提示",{type:'warning'})
                    .then(()=>{//用户点击的是确认按钮
                        //点击确定时执行此处代码，发送ajax请求，将检查项id提交到controller进行处理
                        axios.get("/permission/delete.do?id="+id).then((res)=>{
                            if (!res.data.flag){
                                //删除失败
                                this.$message.error(res.data.message);
                            }else {
                                //删除成功
                                this.$message({
                                    message: res.data.message,
                                    type:'success'
                                });
                                //调用分页查询语句重新查询
                                this.findPage();
                            }
                        }).catch((res)=>{
                            this.showMessage(res);
                        });
                    }).catch(()=>{//用户按了取消按钮
                    this.$message({
                        message: '操作已取消',
                        type:'info'
                    });
                })
            },
            handleUpdate(id){
                //设置编辑窗口属性，dialogFormVisible4Edit为true显示
                this.dialogFormVisible4Edit=true;
                //回显数据，发送ajax请求，根据检查项id提获取当前检查项的信息
                axios.get("/permission/findById.do?id="+id).then((res)=>{
                    if (res.data.flag){

                        //为模型数据设置值，基于vue双向数据绑定进行回显
                        this.permission=res.data.data;

                    }else {
                        this.$message.error("获取数据失败，请刷新当前页面");
                    }
                })
            }
        }
    })
</script>
</html>